package com.example.algorithm.no0004.solution;

import com.example.algorithm.no0004.Solution;

/**
 * @Description 解答-寻找两个正序数组的中位数-暴力解法
 * @ClassName MapSolution
 * @Author Administrator
 * @Date 2020/5/20 10:26
 * @Version 1.0.0
 */
public class SolutionImpl implements Solution {
    @Override
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int length = nums1.length + nums2.length;
        int[] newNums = new int[length];
        int pos = 0;
        int i = 0 , j = 0;
        while(i<nums1.length || j<nums2.length){
            if(i== nums1.length){
                newNums[pos] = nums2[j];
                pos++;
                j++;
            } else if(j== nums2.length){
                newNums[pos] = nums1[i];
                pos++;
                i++;
            } else if(nums1[i] <= nums2[j]){
                newNums[pos] = nums1[i];
                pos++;
                i++;
            } else {
                newNums[pos] = nums2[j];
                pos++;
                j++;
            }
        }
        if(length%2 == 1){
            return newNums[(length-1)/2];
        } else {
            return (newNums[length/2] + newNums[length/2 -1])/2.0;
        }
    }
}
